<?php
ArrowController::importModel("containers.ArrowContainer");
/**
 * Arrow containers List
 * 
 * @version 1.0
 * @license  GNU GPL
 * @author Artur Kmera <artur.kmera@arrowplatform.org>
 * @todo Wprowadzi� keszowanie 
 */
class ArrowContainersList implements IArrowCacheable, IArrowSingleton{
	
	
	private $confDoc;
	/**
	* Configuration from cache provider
	* 
	* @var Array
	*/
	private $configuration;
	
	/**
	 * Instance
	 * 
	 * @var ArrowCointainersList[]
	 */
	private static  $selfInstances = null;

	/**
	 * Singleton !NO_REMOTE
	 * 
	 * @return ArrowContainersList Default instance
	 */
	public static function getDefault( ) {
		if ( self::$selfInstances == null ) {
			self::$selfInstances = new ArrowContainersList( );
		}
		return self :: $selfInstances ;
	}
	
	/**
	 * Constructor !NO_REMOTE
	 * 
	 * @param ArrowProject $projectReference
	 */
	private function __construct( ) {
		$confFile = ArrowProject::PATH . "/" . ArrowProject::CONTAINERS_CONF;
		$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => "containers" ) );
		require_once $cacheFile;
		$this->configuration = $cache;
	}	
	
	/**
	 * IArrowCacheable implementation
	 *
	 * @param Array $params
	 * @return Array
	 */
	public function generateCache($params){
		$project = ArrowController::getDefault()->getProject();
		$templatesStruct = $project->getTemplatesStructure();
		$bansStruct = $project->getBeansStructure();
		$tmp = array();
		$conf = $project->getXMLConfig(ArrowProject::CONTAINERS_CONF);
		$tmp["containers"] = array();
		$tmp["membersConnections"] = array();
		$tmp["membersConnections"]["templates"] = array();
		$tmp["membersConnections"]["beans"] = array();
		foreach( $conf->containers->container as $container ){
			$tmpSchema = array();
			$tmpSchema["name"] = (string) $container["name"];
			
			$tmpVal = (string) $container["auth"];
			$tmpSchema["auth"] = ($tmpVal=="false")?false:true;
			
			$tmpVal = (string) $container["caching"];
			$tmpSchema["caching"] = ($tmpVal=="false")?false:true;
			
			$tmpVal = (string) $container["forceCompile"];
			$tmpSchema["forceCompile"] = ($tmpVal=="false")?false:true;
			
			
			$tmpSchema["cachingTime"] = (string) $container["cachingTime"];
			$tmpSchema["errorTemplate"] = (string) $container["errorTemplate"];
			$tmpSchema["loginTemplate"] = (string) $container["loginTemplate"];
			$tmpSchema["permissionDenyTemplate"] = (string) $container["permissionDenyTemplate"];
			
			//$tmpSchema["members"] = array();
			
			
			
			foreach( $container->templateMembers->template as $member ){
				$id = $templatesStruct->getTemplateIdByMappingPath( (string) $member);
				//$tmpSchema["members"][] = $id;
				$tmp["membersConnections"]["templates"][$id] = (string) $container["name"];
			}
			
			foreach( $container->beanMembers->bean as $member ){
				$id = $templatesStruct->getBeanIdByMappingPath( (string) $member);
				//$tmpSchema["members"][] = $id;
				$tmp["membersConnections"]["beans"][$id] = (string) $container["name"];
			}
			$tmp["containers"][ $tmpSchema["name"] ] = $tmpSchema;
		}
		return $tmp;
	}	
	
	
	/**
	 * Returns container instance by name
	 *
	 * @param String $$cointainerName
	 * @return ArrowContainer
	 */
	public function getContainerByName( $cointainerName ){
		
		if(isset($this->configuration["containers"][$cointainerName]))
			return new ArrowContainer( $this->configuration["containers"][$cointainerName] );
		
		if (empty ($result))
		throw new ArrowException(array (
				"msg" => "Container dosn't exists !.",
				"name" => $cointainerName
				));
		$registred = $result[0];

		
	}
	
	public function getContainers(){
		$tmp = array();
		foreach ( $this->configuration["containers"] as $key => $cont )
			$tmp[] = $this->getContainerByName($key);
		return $tmp;
	}
	
	/**
	 * Returns default cointainer
	 * 
	 * @return ArrowContainer
	 */
	public function getDefaultContainer(){
		return $this->getContainerByName("Default");
	}
	
	/**
	 * Returns parent cointainer of object if can't found returns default
	 * 
	 * @param Object $object ArrowBeanDescriptor || ArrowTemplateDescriptor
	 * @return ArrowContainer
	 */
	public function getParentContainer( $object ){
		if( $object instanceof ArrowBeanDescriptor  ){
			$conn = $this->configuration["membersConnections"]["beans"];
		}elseif( $object instanceof ArrowTemplateDescriptor  ){
			$conn = $this->configuration["membersConnections"]["templates"];
		}
		
		if (!isset($conn[ $object->getId() ]))
			return $this->getDefaultContainer();
		
		return new ArrowContainer( $this->configuration["containers"][$conn[ $object->getId() ]] );		
	}
	
}
?>